<?php
/**
* 名称：产品模块控制器。
* 描述：产品模块相关的控制响应程式。
*/
final class Controller_Product extends Controller_Public_Account{
    /**
    * 描述：列表视图程式。
    * @access protected
    * @return void
    */
    protected function Listview(){
        $data=array(
            'meta'=>array(
                'title'=>'产品管理',
                'button1_title'=>'新增产品',
                'button1_link'=>Route::MakeHyperlink(':product::add'),
                'list_title'=>'产品列表'
            ),
            'base'=>self::$Database->GetLimit(
                'SELECT * FROM '.self::$Database->Table('product').' ORDER BY id DESC',
                Route::GetArguments('page'),
                20
            ),
            'urlArguments'=>array(
                'category'=>'0'
            )
        );
        //~
        foreach($data['base']['Rows'] as &$value){
            $value['category_name']=Model_Product::GetCategoryName($value['category']);
        }
        //~
        self::$Template->Assign('data',$data);
        self::$Template->Display('oui/product/list.html');
    }

    /**
    * 描述：添加之视图程式。
    * @access protected
    * @return void
    */
    protected function Add(){
        $data=array(
            'meta'=>array(
                'title'=>'新增产品',
                'action'=>Route::MakeHyperlink(':product::insert')
            ),
            'category'=>self::$Database->GetAll(
                'SELECT * FROM '.self::$Database->Table('product_category').' ORDER BY id ASC'
            )
        );
        //~
        self::$Template->Assign('data',$data);
        self::$Template->Display('oui/product/form.html');
    }

    /**
    * 描述：添加之响应程式。
    * @access protected
    * @return void
    */
    protected function Insert(){
        $data=array(
            'name'=>post('name'),'description'=>post('description'),
            'picture'=>post('picture'),'category'=>post('category'),
            'label'=>post('label'),'summary'=>post('summary'),'datetime'=>now(TRUE)
        );
        //~
        $this->Feedback(
            self::$Database->Insert(self::$Database->Table('product'),$data),
            array(
                'notice'=>'添加产品成功，3秒钟后自动返回。',
                'link'=>array(
                    array('name'=>'继续添加','href'=>Route::MakeHyperlink(':product::add')),
                    array('name'=>'前往修改','href'=>Route::MakeHyperlink(':product::edit','id:'.self::$Database->lastInsertId())),
                    array('name'=>'返回列表','href'=>Route::MakeHyperlink(':product::listview'))
                ),
                'autoTurn'=>array(TRUE,3)
            ),
            '发布失败，请稍候再试！'
        );
    }

    /**
    * 描述：修改之视图程式。
    * @access protected
    * @return void
    */
    protected function Edit(){
        $data=array(
            'meta'=>array(
                'title'=>'修改产品',
                'action'=>Route::MakeHyperlink(':product::update')
            ),
            'base'=>self::$Database->GetRow('SELECT * FROM '.self::$Database->Table('product').' WHERE id=?',
                array(Route::GetArguments('id'))
            ),
            'category'=>self::$Database->GetAll(
                'SELECT * FROM '.self::$Database->Table('product_category').' ORDER BY id ASC'
            )
        );
        //~
        self::$Template->Assign('data',$data);
        self::$Template->Display('oui/product/form.html');
    }

    /**
    * 描述：修改之响应程式。
    * @access protected
    * @return void
    */
    protected function Update(){
        $data=array(
            'base'=>array(
                'name'=>post('name'),'description'=>post('description'),
                'picture'=>post('picture'),'category'=>post('category'),
                'label'=>post('label'),'summary'=>post('summary'),'datetime'=>now(TRUE)
            ),
            'condition'=>array('sql'=>'id=?','parameters'=>array(
                post('id')
            ))
        );
        //~
        $this->Feedback(
            self::$Database->Update(self::$Database->Table('product'),$data['base'],$data['condition']),
            array(
                'notice'=>'产品修改成功，3秒钟后自动返回。',
                'link'=>array(
                    array('name'=>'继续修改','href'=>Route::MakeHyperlink(':product::edit','id:'.post('id'))),
                    array('name'=>'返回列表','href'=>Route::MakeHyperlink(':product::listview'))
                ),
                'autoTurn'=>array(TRUE,3)
            ),
            '修改失败，请稍候再试！'
        );
    }

    /**
    * 描述：删除之响应程式。
    * @access protected
    * @return void
    */
    protected function Delete(){
        $id=Route::GetArguments('id');
        //~
        $this->Feedback(
            self::$Database->ExecParameterized(
                'DELETE FROM '.self::$Database->Table('product').' WHERE id=?',array($id)
            ),
            $_SERVER['HTTP_REFERER'],
            '删除失败，请稍候再试！'
        );
    }
}